package com.hapoerpejb.services;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.HAPOERP_EJB.persistance.CV;
import com.HAPOERP_EJB.persistance.Offer;
/**
 * Session Bean implementation class CVDAOImpl
 */
@Stateless
public class CVDAOImpl implements CVDAORemote, CVDAOLocal {

    /**
     * Default constructor. 
     */
	@PersistenceContext
	EntityManager em;
	
    public CVDAOImpl() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public CV findCVById(int id) {
		// TODO Auto-generated method stub
		return em.find(CV.class, id);
	}

	@Override
	public void remove(CV ofe) {
		// TODO Auto-generated method stub
		em.remove(em.merge(ofe));
		
	}

	@Override
	public void update(CV ofe) {
		// TODO Auto-generated method stub
		em.merge(ofe);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CV> findAll() {
		// TODO Auto-generated method stub
		return em.createNamedQuery("CV.findAll").getResultList();
	}

	@Override
	public void create(CV ofe) {
		// TODO Auto-generated method stub
		em.persist(ofe);
		
	}

	@Override
	public void deletebyid(int id) {
		// TODO Auto-generated method stub
		em.createNamedQuery("CV.delete").setParameter("var", id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CV> findByOfferid(String string) {
		// TODO Auto-generated method stub
		return em.createNamedQuery("CV.findByOfferid").setParameter("id", string).getResultList();
	}

	@Override
	public List<CV> screeninCv(String id, Offer offre, List<CV> cvs) {
		// TODO Auto-generated method stub
		
		List<CV> cvstock = new ArrayList<CV>();
		int i=0;
		int v=0;
		int nblang=0;
		int total=0;
		int ax=0;
		List<String> Langue = new ArrayList<String>();
		int nbmachines=0;
		List<String> Machines = new ArrayList<String>();
		
		
		if(offre.getLangue().contains("French"))
		{
			nblang++;
			Langue.add("French");
		}
		if(offre.getLangue().contains("English"))
		{
			nblang++;
			Langue.add("English");
		}
		
		if(offre.getCompet().contains("Financial Director"))
		{
			nbmachines++;
			Machines.add("Financial Director");
		}
		if(offre.getCompet().contains("Cash"))
		{
			nbmachines++;
			Machines.add("Cash");
		}
		if(offre.getCompet().contains("Accounting"))
		{
			nbmachines++;
			Machines.add("Accounting");
		}
		if(offre.getCompet().contains("Steward"))
		{
			nbmachines++;
			Machines.add("Steward");
		}
		if(offre.getCompet().contains("Machines"))
		{
			nbmachines++;
			Machines.add("Machines");
		}
		if(offre.getCompet().contains("Conditioning"))
		{
			nbmachines++;
			Machines.add("Conditioning");
		}
		
		
		for(CV cvcomp: cvs)
		{
			for(v=0 ; v < nblang ; v++)
			{
				if(cvcomp.getCompling().contains(Langue.get(v)))
				{
					i++;
				}
			}
			
			for(v=0 ; v < nbmachines ; v++)
			{
				if(cvcomp.getCompfina().contains(Machines.get(v)))
				{
					i++;
				}
			}
			
			for(v=0 ; v < nbmachines ; v++)
			{
				if(cvcomp.getCompinf().contains(Machines.get(v)))
				{
					i++;
				}
			}
			
			for(v=0 ; v < nbmachines ; v++)
			{
				if(cvcomp.getCompind().contains(Machines.get(v)))
				{
					i++;
				}
			}
			
			if(cvcomp.getTitre().contains(offre.getFormationdegree()))
			{
				i++;
			}
			
			if(cvcomp.getExdate().substring(0, 1) != "")
			{
				if(offre.getExperience().substring(0, 1) != "")
				{
					if(Integer.parseInt(cvcomp.getExdate().substring(0, 1)) >= Integer.parseInt(offre.getExperience().substring(0, 1)))
							{
								i++;
								ax++;
								
							}
				}
			}
			
			total = nblang + nbmachines + ax;
			
			if(i >= total/2 )
			{
				cvstock.add(cvcomp);
				i=0;
				ax=0;
				v=0;
			}
		}
		return cvstock;
				
	}

	@Override
	public void deletebyscreen(String screen) {
		// TODO Auto-generated method stub
		em.createNamedQuery("CV.deletscreen").setParameter("var", screen);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CV> findAllbyscreen(String screen,String id) {
		// TODO Auto-generated method stub
		return em.createNamedQuery("CV.findAllbyscreen").setParameter("var", screen).setParameter("var2", id).getResultList();
	}

	
}
